/**
 * 
 */
package vn.lumi.smarthome.network;


import vn.lumi.smarthome.LumiHome;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.database.TBL_Schedule;
import vn.lumi.smarthome.ulti.Preference;
import android.content.Context;
import android.database.Cursor;

/**
 * @author Chu Tuan Anh
 * 
 */
public class UpdateScheduleDeleted extends HCMsg {

	// variables
	private int newConfigVersion = 0;
	private int userId = 0;
	private int scheduleId = 0;
	private boolean isCorrectFormat = false;

	// byte position
	// Author: Hieu sua
	// private static final int userIdPos = 2;
	private static final int userIdPos = 1;

	// End sua
	/**
	 * @param cmd_id
	 * @param Msg_id
	 * @param len
	 */
	public UpdateScheduleDeleted(int userId) {
		super(NetMessage.CMD_HC_UPDATE_SCHEDULE_DELETED, 0);
		// TODO Auto-generated constructor stub
		this.userId = userId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.HCMsg#parseMessage(byte[], int)
	 */
	@Override
	public void parseMessage(byte[] input, int length) {
		// TODO Auto-generated method stub
		isCorrectFormat = isCorrectFormat(input, length);
		if (isCorrectFormat == false)
			return;
		// Author: Hieu sua
		/*
		 * this.userId = Byte2Unsigned(input[1]); this.scheduleId = Byte2Unsigned(input[3]);
		 * this.newConfigVersion= Byte2Unsigned(input[4]);
		 */
		this.scheduleId = Byte2Unsigned(input[2]);
		this.newConfigVersion = Byte2Unsigned(input[3]);
		// End sua

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.HCMsg#doWhenReceived(android.content.Context)
	 */
	@Override
	public void doWhenReceived(Context context) {
		// TODO Auto-generated method stub
		String selection = new String(TBL_Schedule._ID + "=?");
		String[] selectionArgs = new String[] { this.scheduleId + "" };
		// find state ids
		String[] projection = new String[] { TBL_Schedule.STATE_ID };
		Cursor cursor = context.getContentResolver().query(
				TBL_Schedule.CONTENT_URI_SCHEDULE, projection, selection,
				selectionArgs, null);
		int stateId = 0;
		
		if (cursor.getCount() != 0) {
			cursor.moveToFirst();
			stateId = cursor.getInt(cursor
					.getColumnIndex(TBL_Schedule.STATE_ID));
			
			// del state
			context.getContentResolver().delete(
					TBL_DeviceState.CONTENT_URI_DEVICE_STATE,
					new String(TBL_DeviceState._ID + "=?"),
					new String[] { stateId + "" });
			context.getContentResolver()
					.delete(TBL_Schedule.CONTENT_URI_SCHEDULE, selection,
							selectionArgs);
			cursor.close();
		}

		// del Schedule
		// context.getContentResolver().delete(TBL_Schedule.CONTENT_URI_SCHEDULE, selection,
		// selectionArgs);

		// save config in preferences
		LumiHome.HC_Config_ver = this.newConfigVersion;
		Preference.saveConfigVersion(context, this.newConfigVersion);
	}

	@Override
	public boolean isCorrectFormat(byte[] input, int length) {
		boolean result = false;
		if (super.isCorrectFormat(input, length) == false)
			result = false;
		else {
			if (this.userId == Byte2Unsigned(input[userIdPos]))
				result = true;
			else
				result = false;
		}
		return result;
	}
}
